<template>
  <div>
    <!--搜索-->
    <el-card class="box-card">
      <el-form size="small" :inline="true" :model="queryData" class="demo-form-inline" label-width="100px">
        <el-form-item>
            <el-select v-model="queryData.empId" placeholder="选择员工">
                <el-option
                        v-for="item in emp"
                        :key="item.empId"
                        :label="item.empName"
                        :value="item.empId"/>
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="query">查询</el-button>
          <el-button size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
      <div style="height: 30px"></div>
    <!--按钮-->
    <el-card class="box-card">
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="currentHandler">
          添加可用成员
        </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleEdit = true">批量修改限额
        </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="shanchus">批量删除权限
        </el-button>
        <el-button icon="el-icon-plus" @click="download">导出数据
        </el-button>
      </div>
      <!--table-->
      <div class="interspaceH"></div>
<!--        <el-table :data="tableData" style="width: 100%" @selection-change="selsChange" >//@row-click="handleChange"-->
        <el-table @selection-change="tableRowChange" :data="tableData"
                  :header-cell-style="{background:'#F3F4F7',color:'#555'}" style="width: 100%">
            <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="empName" label="员工姓名">
          </el-table-column>
          <el-table-column prop="numMoney" label="单日限额(笔数/总金额)">
          </el-table-column>
          <el-table-column prop="amountday" label="今日已发放金额/剩余金额">
          </el-table-column>
          <el-table-column prop="frequency" label="今日已发放次数/剩余次数">
          </el-table-column>
          <el-table-column prop="numberAmount" label="累计发放(笔数/总金额)">
          </el-table-column>
          <el-table-column label="操作">
              <template slot-scope="scope">
                  <el-button type="text" @click="fafang(scope.row)">发放记录</el-button>
                  <el-button size="mini" type="text" @click="update(scope.$index, scope.row)">修改限额
                  </el-button>
                  <el-button size="mini" type="text" @click="shanchu(scope.row)">删除权限
                  </el-button>
              </template>
          </el-table-column>
        </el-table>
        <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                       @current-change="queryDataHandle($event,pagePagination.pageSize)"
                       :current-page="pagePagination.currentPage"
                       :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                       layout="total, sizes, prev, pager, next"
                       :total="pagePagination.total">
        </el-pagination>

      <!--添加员工-->
      <el-dialog :title="titles" :visible.sync="adds" width="40%" :close-on-click-modal="false">
        <div class="m-alert">
          <div>
            <div>限额以【微信支付商户平台】配置的数值为准，若在企微版配置的限额超出官方配置，执行规则依然以官方配置为准，请合理填写。官方配置入口【微信支付商户平台】——【产品中心】——【企业付娟到零钱】——【产品设置】。</div>
            <div>最大限额参考：</div>
            <div>1、商户号单日出款额度： 10万</div>
            <div>2、单人单日付款额度： 5000元</div>
            <div>单笔付款最低额度： 0.30元，默认每天最多可向同一个用户付款10次</div>
          </div>
        </div>
        <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="200px" class="demo-ruleForm">
          <el-form-item label="可用成员" prop="name">
              <el-select v-model="formData.empId" placeholder="请选择可用员工">
                  <el-option
                          v-for="item in emp"
                          :key="item.empId"
                          :label="item.empName"
                          :value="item.empId" />
              </el-select>
          </el-form-item>
          <el-form-item label="单日员工发放红包次数" prop="sendNum">
            <el-input v-model="formData.sendNum" placeholder="请输入范围1-99999只能填写整数" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="单日员工发放红包金额" prop="sendMoney">
            <el-input v-model="formData.sendMoney" placeholder="输入范围0.30-5000.00，保留两位小数" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="quxiao">取消</el-button>
            <el-button size="small" type="primary" @click="queding">确定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!--编辑-->
      <el-dialog :title="title" :visible.sync="edit" width="30%" :close-on-click-modal="false">
          <div class="m-alert">
              <div>
                  <div>限额以【微信支付商户平台】配置的数值为准，若在企微版配置的限额超出官方配置，执行规则依然以官方配置为准，请合理填写。官方配置入口【微信支付商户平台】——【产品中心】——【企业付娟到零钱】——【产品设置】。</div>
                  <div>最大限额参考：</div>
                  <div>1、商户号单日出款额度： 10万</div>
                  <div>2、单人单日付款额度： 5000元</div>
                  <div>单笔付款最低额度： 0.30元，默认每天最多可向同一个用户付款10次</div>
              </div>
          </div>
        <el-form :model="editData" :rules="rules" ref="ruleForm" label-width="170px" class="demo-ruleForm">
          <el-form-item label="单日员工发放红包次数" prop="sendNum">
            <el-input v-model="editData.sendNum"></el-input>
          </el-form-item>
          <el-form-item label="单日员工发放红包金额" prop="sendMoney">
            <el-input v-model="editData.sendMoney"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="cancel">取消</el-button>
            <el-button size="small" @click="submit">保存</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
<!--  批量修改-->
        <el-dialog title="批量修改" :visible.sync="handleEdit" width="30%" :close-on-click-modal="false">
            <div class="m-alert">
                <div>
                    <div>限额以【微信支付商户平台】配置的数值为准，若在企微版配置的限额超出官方配置，执行规则依然以官方配置为准，请合理填写。官方配置入口【微信支付商户平台】——【产品中心】——【企业付娟到零钱】——【产品设置】。</div>
                    <div>最大限额参考：</div>
                    <div>1、商户号单日出款额度： 10万</div>
                    <div>2、单人单日付款额度： 5000元</div>
                    <div>单笔付款最低额度： 0.30元，默认每天最多可向同一个用户付款10次</div>
                </div>
            </div>
            <el-form :model="editsData" :rules="rules" ref="ruleForm" label-width="160px" class="demo-ruleForm">
                <el-form-item label="单日员工发放红包次数" prop="danri">
                    <el-input v-model="editsData.sendNum"></el-input>
                </el-form-item>
                <el-form-item label="单日员工发放红包金额" prop="danri">
                    <el-input v-model="editsData.sendMoney"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="small" @click="handleEdit = false">取消</el-button>
                    <el-button size="small" @click="auditServer">保存</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </el-card>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"
import axios from "axios";
export default {
  components: {
    Pagination
  },
  data() {
    return {
        emp: [],
        //批量操作
        sels:[],
      tableData: [],
        pagePagination: {
            currentPage: 1,
            pageSizes: [10, 15, 20, 25],
            pageSize: 10,
            total: 0
        },
        handleEdit: false,
      formData: {
        empId: '',
          sendNum: '',
          sendMoney: ''
      },
      editData: {
          sendNum: '',
          sendMoney: ''
      },
      queryData: {
            empId:''
      },
      //分页开始
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 4
      },

      formInline: {
        page: 1,
        limit: 10,
        varLable: '',
        varName: '',
        token: localStorage.getItem('logintoken')
      },
      //分页结束
      //添加开始
      titles: '添加',
      title: '编辑',
      adds: false,
      edit: false,
      rules: {
        empId: [
          { required: true, message: '请选择员工', trigger: 'blur' }
        ],
          sendNum: [
          { required: true, message: '请输入范围1-99999只能填写整数', trigger: 'blur' }
        ],
          sendMoney: [
          { required: true, message: '输入范围0.30-5000.00，保留两位小数', trigger: 'blur' }
        ]
      },
      editsData: {},
      //添加结束
    }
  },
    mounted() {
        this.queryDataHandle(1, 10)
    },
  methods:{
      queryDataHandle(page = 1, row = 10) {
          let payload = {
              pageNum: page,
              pageSize: row,
              empId: this.queryData.empId
          }
          console.log(payload)
          let that = this
          that.$request("/api/available/getByUserName/1_0", { }, "POST").then((data) => {
              that.pagePagination['total'] = data.total
              that.pagePagination['pageSize'] = data.row
              that.tableData = data.rows
          })

          //查询员工
          var emp = {
              empId: ''
          }
          this.$request("/api/emp/getEmpList/1_0", emp, "POST").then((data) => {
              that.emp = data.rows
          })
      },
      query() {
          let available = {
              empId : this.queryData.empId
          }
          let that = this
          that.$request("/api/available/getByUserName/1_0", available, "POST").then((data) => {
              that.tableData = data.rows
          })
      },
      queding() {
          let available = {
              empId : this.formData.empId,
              sendNum: this.formData.sendNum,
              sendMoney: this.formData.sendMoney
          }
          let that = this
          that.$request("/api/available/addAvailable/1_0", available, "POST").then((data) => {
              this.$message({
                  message: '添加成功',
                  type: 'success'
              })
              this.adds = false
              this.queryDataHandle(this.pagePagination.currentPage, this.pagePagination.pageSize)
          })
      },
      fafang(item) {
          this.$emit('dataToSon', item)
      },
      shanchu(item) {
          this.$confirm('确定要删除吗?', '信息', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          })
              .then(() => {
                  debugger
                  this.$request("/api/available/deleteUserName/1_0", item, "DELETE").then((data) => {
                      this.$message({
                          message: '删除成功',
                          type: 'success'
                      })
                  })
                  //this.queryDataHandle(this.pagePagination.currentPage, this.pagePagination.pageSize)
              })
              .catch(() => {
                  this.$message({
                      type: 'info',
                      message: '已取消删除'
                  })
              })
      },

    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //分页结束
    //添加开始
    currentHandler() {
          this.formData = {}
      this.adds = true
    },
    quxiao() {
      this.adds = false
    },

    //添加结束
  //编辑开始
    update(index, row) {
      this.edit = true
      this.editData = row
    },
    cancel() {
      this.edit = false
    },
    submit() {
          console.log(this.editData)
        let available = {
            availableId : this.editData.availableId,
            sendNum: this.editData.sendNum,
            sendMoney: this.editData.sendMoney
        }
        let that = this
        that.$request("/api/available/updateNumber/1_0", available, "POST").then((data) => {
            this.$message({
                message: '保存成功',
                type: 'success'
            })
            this.edit = false
            this.queryDataHandle(this.pagePagination.currentPage, this.pagePagination.pageSize)
        })

    },
  //编辑结束


    resetQuery() {
        this.queryData = {}
    },
      tableRowChange(val) {
          if (val.length > 0) {
              this.titleCurrentPanel = true
          } else {
              this.titleCurrentPanel = false
          }
          this.multipleSelection = val;
      },

    download() {
        let available = {empId : this.queryData.empId}
        this.$requestFile("/api/available/exportTable/1_0", available, "可用红包员工表.xlsx")

        /*axios.post("/api/available/exportTable/1_0", available, {responseType: 'blob'},
            {
                headers: {
                    "content-type": "multipart/from-data",
                }
            }).then((res) => {
            // 创建a标签
            const link = document.createElement('a')
            //设置下载类型
            let blob = new Blob([res.data], {})
            //设置a标签样式
            link.style.display = 'none'
            //设置a标签url
            link.href = URL.createObjectURL(blob)

            //设置下载后文件名
            link.download = "可用员工.xlsx"
            //页面添加创建的a标签
            document.body.appendChild(link)
            //点击a标签
            link.click()
            //删除a标签
            document.body.removeChild(link)
        }).catch(error => {
            this.$message.error('下载失败');
        })*/
    },
      selsChange(sels) {
          this.sels = sels
      },
      // handleChange(row, event, column) {
      //     this.$refs.multipleTable.toggleRowSelection(row)
      // },
      auditServer () {
          var availableId = this.sels.map(item => item.availableId).join(",")
          var available = {
              availableId:availableId,
              sendNum: this.editsData.sendNum,
              sendMoney: this.editsData.sendMoney
          }
          let that = this
          that.$request("/api/available/updateNumbers/1_0", available, "POST").then((data) => {
              this.$message({
                  message: '保存成功',
                  type: 'success'
              })
              this.handleEdit = false
              this.queryDataHandle(this.pagePagination.currentPage, this.pagePagination.pageSize)
          })
      },
      shanchus() {
          var availableId = this.sels.map(item => item.availableId).join(",")
          var available = {
              availableId:availableId
          }
          let that = this
          that.$request("/api/available/deleteUserNames/1_0", available, "DELETE").then((data) => {
              this.$message({
                  message: '保存成功',
                  type: 'success'
              })
              this.handleEdit = false
              this.queryDataHandle(this.pagePagination.currentPage, this.pagePagination.pageSize)
          })
      },
      toggleSelection(){
          this.$refs.multipleTable.clearSelection();
      }

  }
}
</script>

<style scoped>

</style>
